#include <header.h>

void my_read(int file_fd, char *buf, int len){

    int size = 0;
    while(size < len){
        ssize_t ret = read(file_fd, buf+size, len-size);
        size+=ret;
    }
}
int main(){

    while(1){
        int file_fd = open("1.txt", O_WRONLY);
        char buf[100] = {0};
        ssize_t ret = read(STDIN_FILENO, buf, sizeof(buf) );
        write(file_fd, &ret, sizeof(ret));
        write(file_fd, buf, ret);
        close(file_fd);


        int file_fd2 = open("2.txt", O_RDWR);
        ssize_t num = 0;
        my_read(file_fd2, (char *)&num, sizeof(ssize_t));
        bzero(buf, sizeof(buf));
        my_read(file_fd2, buf, num);
        printf("buf: %s \n", buf);
        ftruncate(file_fd2, 0);
        close(file_fd2);
    }
    return 0;
}

